# -*- coding: utf-8 -*-

'''eof
name:申请企业行业校验
code:R04_EG_IndLimitCheck_ZC
tableName:
columnName:
groups:企业业务信息校验
dependencies:TX_CQ_DSJ,EG_DSJ,CQB_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import json

reload(sys)
sys.setdefaultencoding('utf-8')


def FormatData(tableName):
    try:
        ic = CQB_DSJ.get("data").get(tableName)
        return ic if isinstance(ic, list) else [{}]
    except:
        return [{}]


def is_num(num):
    try:
        float(num)
        return True
    except:
        return False


def get_hydm():
    try:
        try:
            hydm = TX_CQ_DSJ["data"]["syptGsdjxx"][0]["HY_DM"]
            if hydm in null_type_list:
                hydm = EG_DSJ["data"]["R1103V3"][0]["basicList"][0]["industryCode"]
        except:
            hydm = EG_DSJ["data"]["R1103V3"][0]["basicList"][0]["industryCode"]
        return hydm
    except:
        return None


null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getRule04():
    try:
        hydm = get_hydm()
        depositInfo = FormatData("industryInfo")
        if depositInfo == [{}]:
            raise RuntimeError("未能获取到行业信息")
        else:
            if len(depositInfo) >= 1:
                for i in depositInfo:
                    deposit = i.get("isInTransit")
                    type = i.get("type")
                    if hydm not in null_type_list:
                        if deposit not in null_type_list and deposit == u"Y":
                            return type
                        elif deposit not in null_type_list and deposit == u"N":
                            return 1
                    else:
                        if deposit not in null_type_list and deposit == u"Y":
                            return type
                        elif deposit not in null_type_list and deposit == u"N":
                            raise ValueError("行业代码为空且未触碰;isInTransit = " + deposit)
                    raise ValueError("行业信息准入信息格式错误;deposit = " + deposit)
            else:
                raise RuntimeError("行业信息为空")
    except:
        raise


if __name__ == "__main__":
    file_obj = open(r'../data/rating/R04_EG_IndLimitCheck/CQB_DSJ.json', 'r')
    content = file_obj.read()
    CQB_DSJ = json.loads(content, strict=False)
    aa = getRule04()
    print aa

result = getRule04()
